package com.rtx.wcs.ecs.ristowcs;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rtx.wcs.std.bean.po.RtxPalletcantilever;
import com.rtx.wcs.std.handle.ristowcs.rfagv.AbsRFAGVTaskDownHandle;
import com.rtx.wcs.std.service.RtxPalletcantileverService;


/**
 * @author Administrator
 *
 */
@Service("RF-AGV")
public class RFAGV extends AbsRFAGVTaskDownHandle{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	RtxPalletcantileverService rtxPalletcantileverService;
	
	@Value("${ecs.task.down.url}")
	private String RF_CTU_URL;
	

	//{				
	//    "code": “0”, 				
	//    "data": "F01169C808C317111G",				
	//    "message": "成功", 				
	//    "reqCode": "468513"				
	//	}
	//
	@Override
	protected Map<String, String> handleResponse(RtxPalletcantilever o,String response,String mappingcode) {
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("editflag", "");//PS为成功 PE为失败
		resultMap.put("message", "");
		resultMap.put("data", "");
		resultMap.put("status", "");
		
		try {
			JSONObject res = JSON.parseObject(response);
			if("0".equals(res.getString("code"))) {
				resultMap.put("editflag", "PS");
			}else {
				resultMap.put("editflag", "PE");
			}
			resultMap.put("message", res.getString("message"));
			resultMap.put("data", res.getString("data"));
		} catch (Exception e) {
			logger.error("",e);
			resultMap.put("message", response);
			resultMap.put("editflag", "PE");
		}
		return resultMap;
	}





	//示例报文
	//{"reqCode":"468513","reqTime":"","clientCode":"","tokenCode":"","taskTyp":"B02","ctnrTyp":"1","ctnrCode":"10001","ctnrNum":"","taskMode":"","wbCode":"","positionCodePath":[{"positionCode":"p01","type":"05"}],"podCode":"","podDir":"","podTyp":"","materialLot":"","materialType":"","priority":"1","agvCode":"10001","taskCode":"","groupId":"","agvTyp":"","positionSelStrategy ":"","data":""}
	@Override
	protected JSONObject converWcsRequest(RtxPalletcantilever t,String mappingcode) {
		String uid = UUID.randomUUID().toString();
		JSONObject edirequest = new JSONObject();
		edirequest.put("whseid", t.getWhseid());
		edirequest.put("reqCode", uid);
		edirequest.put("reqTime", System.currentTimeMillis()+"");
		edirequest.put("clientCode", "");
		edirequest.put("tokenCode", "");
		if("上架".equals(t.getType())) {
			edirequest.put("taskTyp", "SJ");
			edirequest.put("ctnrTyp", "ID");
		}else if("搬运".equals(t.getType())){
			edirequest.put("ctnrTyp", "TP");
			edirequest.put("taskTyp", "BY");
		}
		edirequest.put("ctnrCode", t.getLpn());
		edirequest.put("ctnrNum", "");
		edirequest.put("taskMode", "");
		edirequest.put("wbCode", "");
		
		JSONObject positionCodePath = new JSONObject();
		positionCodePath.put("positionCode", t.getPositioncode());
		positionCodePath.put("type", t.getLoc());
		edirequest.put("positionCodePath", positionCodePath);
		
		edirequest.put("data", "");
		return edirequest;
	}


	@Override
	protected String getEdiRequestURL(String mappingcode) {
		return RF_CTU_URL;
	}

}
